package com.fdm.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fdm.controller.bankcommand.BankCommandFactory;
import com.fdm.controller.bankcommand._BankManagerCommand;
import com.fdm.controller.properties.BankControllerProperties;
import com.fdm.model.BankManager;
import com.fdm.model._BankManager;
import com.fdm.model.exceptions.BankCommandException;
import com.fdm.model.exceptions.BankManagerException;

public class BankServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1017142876207791240L;

	@Override
	public void doPost(HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		_DisplayManager display = new DisplayManager(request,response);
		_BankManager bankManager = new BankManager();

		try {
			String commandString = request.getParameter("command");
			String className = new BankControllerProperties().getProperty(commandString);
			String message = new BankControllerProperties().
					getProperty(commandString+"_MESSAGE","DEFAULT_MESSAGE");
			_BankManagerCommand command = new BankCommandFactory().createCommand(className);
			command.setAttributes(bankManager, request, display, message);
			command.execute();
		} catch (BankManagerException e) {
			display.displayError(new Date()+" | "+e.getMessage());
		} catch (BankCommandException e) {
			display.displayError(new Date()+" | "+e.getMessage());
		}
	}
}
